<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>step2</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="../lib/layui-v2.5.5/css/layui.css" media="all">
  <link rel="stylesheet" href="../css/public.css" media="all">
</head>

<body>
  <div class="layuimini-container">
    <div class="layuimini-main">
      <div class="layui-form layuimini-form">
        <div class="layui-form-item">
          <label class="layui-form-label required">申报编号：</label>
          <div class="layui-input-block">
            <input type="text" id="declare_num" name="declare_num" lay-verify="required" autocomplete="off" lay-reqtext="申报编号不能为空" placeholder="请输入申报编号" value="" class="layui-input">
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">申报类型：</label>
          <div class="layui-input-block layui-form" lay-filter="selectFilter">
            <select id="declare_type" name="declare_type">
              <option value="固定资产">固定资产</option>
              <option value="首台套">首台套</option>
            </select>
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label required">申报标题：</label>
          <div class="layui-input-block">
            <input type="text" id="declare_title" name="declare_title" lay-verify="required" autocomplete="off" lay-reqtext="申报标题不能为空" placeholder="请输入申报标题" value="" class="layui-input">
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label required">环节状态：</label>
          <div class="layui-input-block">
            <input disabled type="text" id="link_status" name="link_status" lay-verify="required"  lay-reqtext="申报标题不能为空" placeholder="请输入申报标题" value="" class="layui-input">
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label required">申报时间：</label>
          <div class="layui-input-block">
            <input type="text" id="declaration_time" name="declaration_time" value="" autocomplete="off" class="layui-input">
          </div>
        </div>
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 50px;">
          <legend>申报指南：</legend>
        </fieldset>
        <button type="button" class="layui-btn layui-btn-primary" id="upload_step2">
          <i class="layui-icon">&#xe67c;</i>附件上传
        </button>
        <table class="layui-hide" id="table_step2_2" lay-filter="currentTableFilter_step2"></table>

        <div class="layui-form-item">
          <div style="text-align:center">
<!--            <button class="layui-btn layui-btn-primary" lay-submit lay-filter="drop" id="stop">驳回</button>-->
            <button class="layui-btn layui-btn-normal" id="report">提报</button>
          </div>
        </div>
      </div>
    </div>
  </div>
  <script type="text/html" id="currentTableBar_step2">
    <a class="layui-btn layui-btn-xs layui-btn data-count-handle" lay-event="download">下载</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger data-count-handle deletea" lay-event="delete">删除</a>
  </script>
  <script src="../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
  <script src="../lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
  <script src="../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
  <script src="../js/ManualPaging.js"></script>
  <script src="../js/util.js"></script>
  <script>
    if (!localStorage.getItem("userName")) {
      window.location = './login-1.html';
    }
    layui.use(['form', 'table', 'laydate', 'upload', 'miniTab'], function() {
      var $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        laydate = layui.laydate,
        upload = layui.upload,
        miniTab = layui.miniTab

      // 日期组件
      laydate.render({
        elem: '#declaration_time' //指定元素
      });
      // 初始值
      // //根据日期时间
      // let addzero =function(zero){
      //   let index = zero
      //           if(index<10){
      //             zero='0'+index
      //   }
      // }
      let insdata = function(){
        let myDate = new Date;
        let year = myDate.getFullYear(); //获取当前年
        let mon = myDate.getMonth() + 1; //获取当前月
        if(mon<10){
          mon='0'+mon
            }
        let date = myDate.getDate();
        if(date<10){
          date='0'+date
        }
        let h = myDate.getHours();
        if(h<10){
          h='0'+h
        }
        let min = myDate.getMinutes();
        if(min<10){
          min='0'+min
        }
        let s = myDate.getSeconds();
        if(s<10){
          s='0'+s
        }
     $('#declare_num').val(year+''+mon+''+date+''+h+''+min+''+s);
        //同步申报标题
        $.ajax({
          url: publicurl + 'pro/getProcessInstanceByTaskID',
          type: 'GET',
          data: param,
          dataType: 'text',
          success: function(result) {
            let tielet=jQuery.parseJSON(result);

            console.log(tielet)
            console.log(tielet.obj)
            $('#declare_title').val(tielet.obj.name)
          },
          error: function(err) {
            console.log(err)
          }
        });
    }
      $("input[name='link_status']").val(decodeURI(getQueryVariable('name')));
      let url = 'pro/queryJmDeclareByTaskID'
      let param = {
        taskID: getQueryVariable('id') // 获取url参数-
      }
      if (getQueryVariable('type') === '2') {
        url = 'pro/queryJmDeclareByProcessInstanceID'
        param = {
          processInstanceID: getQueryVariable('id')
        }
        // TODO 删除上传，删除按钮
        $("#report").remove();
        $("#upload_step2").remove();
        $("#stop").remove();
        $('#declare_num').attr("disabled", true);
        $('#declare_type').attr("disabled", true);
        $('#declare_title').attr("disabled", true);
        $('#declaration_time').attr("disabled", true);
      }

      // 获取数据
      $.ajax({
        url: publicurl + url,
        type: 'GET',
        data: param,
        dataType: 'text',
        success: function(result) {
          //表单初始赋值
          if (Object.prototype.toString.call(result) == "[object String]") {
            result = JSON.parse(result)
          }
          if (result.obj.declare) {
            // $('#declare_type').attr('value', result.obj.declare.declare_type);
            $("#declare_type").find("option[value='" + result.obj.declare.declare_type + "']").attr("selected", true);
            form.render('select', 'selectFilter');
            $("input[name='declare_num']").val(result.obj.declare.declare_num);
            $("input[name='declare_title']").val(result.obj.declare.declare_title);
            $("input[name='link_status']").val(decodeURI(getQueryVariable('name')));
            $("input[name='declaration_time']").val(result.obj.declare.declaration_time);
          }

          // 表格初始值
          let params = {
            id: '#table_step2_2',
            data: 'step2',
            tool: '#currentTableBar_step2',
            table,
            result
          }
          insdata();
          renderTable(params)
          // 删除 删除按钮
          if (getQueryVariable('type') === '2') {
            $("a").remove(".deletea");
          }
        },
        error: function(err) {
          console.log(err)
        }
      });
      // 下载和删除
      table.on('tool(currentTableFilter_step2)', function(obj) {
        if (obj.event === 'download') { // 监听下载操作
          var aEle = document.createElement("a"); // 创建a标签
          aEle.href = publicurl + 'pro/downLoad?id=' + obj.data.id + '&fileName=' + obj.data.name;
          aEle.click(); // 设置点击事件
        } else if (obj.event === 'delete') { // 监听删除操作
          layer.confirm('是否删除？', function(index) {
            $.ajax({
              url: publicurl + 'pro/deleteJmEnclosure/',
              type: 'GET',
              data: {
                id: obj.data.id
              },
              dataType: 'text',
              success: function(result) {
                obj.del();
                layer.close(index);
               // window.location.reload()
              }
            })
          });
        }
      });
      //驳回
      $("#stop").click(function() {
        layer.open({
          content: '驳回成功'
        });
      })


      // 提报
      $("#report").click(function() {
        if ($(declare_num).val() == "") {
          layer.open({
            content: '请输入申报编号'
          });
        } else if ($(declare_title).val() == "") {
          layer.open({
            content: '请输入申报标题'
          });
        } else if ($(declaration_time).val() == "") {
          layer.open({
            content: '请输入申报时间'
          });
        }
        else {
          if ($(".layui-table-body.layui-table-main")[0].innerText != "无数据") {
            localStorage.setItem("TBaoID", 1);
            $.ajax({
              type: "post",
              url: '' + publicurl + 'task/completeTask',
              async: false,
              data: JSON.stringify({
                declaration_time: $('#declaration_time').val(),
                link_status: $('#link_status').val(),
                declare_title: $('#declare_title').val(),
                declare_type: $('#declare_type').val(),
                declare_num: $('#declare_num').val(),
                taskID: getQueryVariable('id'),
                isSuccess: "true"
              }),
              headers: { 'Content-Type': 'application/json' },
              success: function(res) {
                //关闭当前页面并且切换到ToDoTasks页
                var ele = $(".layuimini-tab .layui-tab-title li.layui-this", parent.document);
                if (ele.length > 0) {
                  var layId = $(ele[0]).attr('lay-id');
                  miniTab.openNewTabByIframe({
                    href:"page/ToDoTasks.html",
                    title:"代办任务",
                  });
                  miniTab.delete(layId, true);
                }
              },
              error: function(request) {
                layer.alert(request.responseJSON.obj, {
                  icon: 2,
                  title: "提示"
                });
              }
            });
          } else {
            layer.open({
              content: '请上传文件'
            });
          }
        }
      })

      // 文件上传
      var uploadInst = upload.render({
        elem: '#upload_step2', //绑定元素
        url: '/pro/uploadFile/', //上传接口  
        data: {
          taskID: getQueryVariable('id')
        },
        done: function(res) {
          //上传完毕回调
          layer.open({
            content: '上传成功',
            time: 1500
          });
          // 上传成功后重新渲染表格
          $.ajax({
            url: publicurl + url,
            type: 'GET',
            data: {
              taskID: getQueryVariable('id') // 获取url参数
            },
            dataType: 'text',
            success: function(result) {
              if (Object.prototype.toString.call(result) == "[object String]") {
                result = JSON.parse(result)
              }
              console.log(result)
              let params = {
                id: '#table_step2_2',
                data: 'step2',
                tool: '#currentTableBar_step2',
                table,
                result
              }
              console.log(result)
              console.log(params)

              renderTable(params)
            },
            error: function(err) {
              console.log(err)
            }
          });
        },
        error: function() {
          //请求异常回调
          layer.open({
            content: '上传失败',
            time: 1500
          });
          setTimeout(() => {
            window.location.reload()
          }, 1500)
        },
        accept: 'file'
      });


    });
  </script>
</body>

</html>